﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSSL.X509;
using Sicurezza.Entita;
using Sicurezza.Client.Manager;
using System.Diagnostics;

namespace Sicurezza.Client.Controlli
{
    public partial class LoadFileControl : UserControl
    {
        private X509Certificate certificate = null;
        List<string> files = new List<string>();


        public LoadFileControl()
        {
            InitializeComponent();
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK
                || string.IsNullOrEmpty(openFileDialog1.FileName))
                return;

            Reset();
            LoadFile(openFileDialog1.FileName);

        }

        private void LoadFile(string filepath)
        {
            SignedMessage sm = null;
            MailMessage mm = null;
            try
            {
                sm = SignedMessageManager.Load(filepath);          
                mm = MailManager.Get(sm.Message);
            }
            catch (ManagerException ex)
            {
                MsgBox.Error(ex.Message);
                return;
            }
            X509Certificate senderCertificate = X509Manager.Get(mm.SenderSerial);
            sm.Message = sm.Message.Replace("\n", "\r\n");

            try
            {
                SSLManager.CheckSign(sm, senderCertificate);
            }
            catch (ManagerException ex)
            {
                MsgBox.Error(ex.Message);
                return;
            }

            X509Certificate recipient = X509Manager.Get(mm.RecipientSerial.ToString());

            PasswordForm pf = new PasswordForm();
            pf.DialogText = "Insert password of identity " + mm.SenderSerial +
                " - " + recipient.Subject.ToString();

            if (pf.ShowDialog() != DialogResult.OK || string.IsNullOrEmpty(pf.Password))
                return;



            X509Certificate sender = X509Manager.Get(mm.SenderSerial.ToString());
            files = MailManager.GetFiles(mm, recipient, pf.Password);

            signedCheck.Checked = sm.Signature != null;
            encipheredCheck.Checked = mm.EncipheredKey != null;

            senderdnText.Text = X509Manager.GetDN(sender);
            senderemailText.Text = X509Manager.GetEmail(sender);

            fileBox.DataSource = files;

            saveFilesButton.Enabled = true;

        }

        public void Reset()
        {
            senderdnText.Text = "";
            senderemailText.Text = "";
            signedCheck.Checked = false;
            encipheredCheck.Checked = false;
            fileBox.DataSource = null;
            fileBox.Items.Clear();
            certificate = null;
            saveFilesButton.Enabled = false;

            FileManager.Delete(files);
            files.Clear();
        }

        private void fileBox_DoubleClick(object sender, EventArgs e)
        {
            Process.Start(fileBox.SelectedValue.ToString());
        }

        private void saveFilesButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK
                || string.IsNullOrEmpty(folderBrowserDialog1.SelectedPath))
                return;

            FileManager.Copy(files, folderBrowserDialog1.SelectedPath);

            MsgBox.Info("Files succesful saved");
        }
    }
}
